package com.foreverwin.jzymes.report.point.service.impl;


import com.foreverwin.jzymes.common.service.ISiteAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.qc.point.domain.PointChecklist;
import com.foreverwin.jzymes.qc.point.enums.PointCheckTypeEnum;
import com.foreverwin.jzymes.qc.point.mapper.FindUndoneMapper;
import com.foreverwin.jzymes.qc.sample.domain.SampleData;
import com.foreverwin.jzymes.qc.sample.mapper.SampleAdiviceMapper;
import com.foreverwin.jzymes.report.point.mapper.PointCheckReportMapper;
import com.foreverwin.jzymes.report.point.service.IPointCheckReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class PointCheckReportServiceImpl implements IPointCheckReportService {
    @Autowired
    PointCheckReportMapper pointCheckReportMapper;
    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    SampleAdiviceMapper sampleAdiviceMapper;
    @Autowired
    FindUndoneMapper findUndoneMapper;


    /**
     * 领料单
     * @param jsonMap
     * @return
     */
    public List<PointChecklist> findPointCheckReport(Map<String,Object> jsonMap ){
        String site = CommonMethods.getSite();
        siteAdviceService.existSite( site );

        String type = ( String ) jsonMap.get("checkType");
        Assert.isTrue( !StringUtils.isBlank( type ) );
        String number = ( String ) jsonMap.get("number");

        if (  !StringUtils.isBlank( number ) ){
            if ( type.equals( PointCheckTypeEnum.SAMPLE.getValue() ) ){
                number = BOHandleUtil.buildSampleHandle( site , number );
            }else {
                number = "ToolNumberBO:" + site + "," + number;
            }
        }

        Date startDate = null;
        Date endDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if ( (String) jsonMap.get( "startDate" ) != "" ){
            try {
                startDate = sdf.parse( (String) jsonMap.get( "startDate" ) );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if ( (String) jsonMap.get( "endDate" ) != "" ){
            try {
                endDate = sdf.parse( (String) jsonMap.get( "endDate" ) );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

      List<PointChecklist> rtnList = pointCheckReportMapper.selectPointCheckByReport(  number , type ,startDate ,endDate );
        for ( PointChecklist pointChecklist : rtnList ){
            changeStatus( pointChecklist );
        }

        return rtnList;
    }

    /**
     *
     * @param type
     * @return
     */
    @Override
    public  List< Map<String,Object> > findNumber(String type) {
        String site = CommonMethods.getSite();
        siteAdviceService.existSite( site );
        List< Map<String,Object> > rtnList = new ArrayList<>();

        if ( type.equals( PointCheckTypeEnum.SAMPLE.getValue() ) ){
          List<SampleData> sampleDataList =  sampleAdiviceMapper.searchSampleData( site , "" , "" );

          for ( SampleData sampleData : sampleDataList ){
              Map<String,Object> map = new HashMap<>();

              map.put( "number" , sampleData.getSampleNo() );
              map.put("description","");
              rtnList.add( map );
          }

        }else {

        String status = "StatusBO:"+site+",302";  //禁用
        List< Map<String,Object> > toolList =  findUndoneMapper.findToolOn( site , status , "");
        for ( Map<String,Object> Bean : toolList ){
            Map<String,Object> map = new HashMap<>();

            map.put( "number" , Bean.get("TOOL_NUMBER"));
            map.put("description",Bean.get("DESCRIPTION") );
            rtnList.add( map );
        }

        }
        return rtnList;
    }

    public void  changeStatus( PointChecklist pointChecklist ){
        if ( pointChecklist.getStatus().equals("R")){
            pointChecklist.setStatus( "提醒" );
        }else if ( pointChecklist.getStatus().equals("O") ){
            pointChecklist.setStatus( "已启用" );
        }else if ( pointChecklist.getStatus().equals("U") ){
            pointChecklist.setStatus( "逾期" );
        }else if ( pointChecklist.getStatus().equals("NG") ){
            pointChecklist.setStatus( "报废" );
        }else if ( pointChecklist.getStatus().equals("RE") ){
            pointChecklist.setStatus( "允收" );
        }
    }
}
